package com.hualan.sfzy.controller;

import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.model.entity.CourseOrder;
import com.hualan.sfzy.service.CourseOrderService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/courseOrder")
public class CourseOrderController {
    @Autowired
    private CourseOrderService courseOrderService;

    @PutMapping
    public Result updateCourseOrder(@RequestBody CourseOrder courseOrder) {
        int i = courseOrderService.edit(courseOrder);
        if (i > 0) {
            return Result.success();
        }
        return Result.error("添加失败");
    }

    @PostMapping
    public Result addCourseOrder(@RequestBody CourseOrder courseOrder) {
        int i = courseOrderService.add(courseOrder);
        if (i > 0) {
            return Result.success();
        }
        return Result.error("修改失败");
    }

    @DeleteMapping("{id}")
    public Result deleteCourseOrder(@PathVariable int id) {
        int i = courseOrderService.remove(id);
        if (i > 0) {
            return Result.success();

        }
        return Result.error("删除失败");
    }

    @DeleteMapping()
    public Result deleteCourseOrders(@RequestParam Integer[] ids) {
        int i = courseOrderService.removeBatch(ids);
        if (i > 0) {
            return Result.success();

        }
        return Result.error("删除失败");
    }

    @GetMapping("/all")
    public Result getAllCourseOrders(@RequestParam(value = "pageNum", defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
                                     CourseOrder courseOrder) {
        Map<String, Object> map = courseOrderService.findPage(pageNum, pageSize, courseOrder);

        return Result.success(map);

    }
    @GetMapping
    public Result getCourseOrders(@RequestParam Integer id) {
        CourseOrder courseOrder = courseOrderService.findById(id);
        return Result.success(courseOrder);
    }
}
